// 注册组件
Vue.component('v-select', VueSelect.VueSelect);
window.onload=function ()
{
    var vue=new Vue({
        el:"#app",
        data:{
            itemCatList:[],
            grade:1,
            item1:{},
            item2:{},
            itemCat:{},
            typeTemplateList:[],
            ids:[],
            checked:false
        },
        methods:{
            findByParentId:function (parentId)
            {
                this.ids=[];
                axios.get("/itemCat/findByParentId?parentId="+parentId).then(function (response)
                {
                    vue.itemCatList=response.data;
                });
                this.itemCat.parentId=parentId;
            },
            nextList:function (itemCat, grade)
            {
                this.grade=grade;
                console.log(this.grade);
                //根据等级实现面包屑显示
                if(grade==1)
                {
                    this.item1={};
                    this.item2={};
                }
                else if (grade==2)
                {
                    this.item1=itemCat;
                    this.item2={};
                }
                else
                {
                    this.item2=itemCat;
                }

                //根据parentId查找下一列数据
                this.findByParentId(itemCat.id);
            },
            findTypeTemplateList:function ()
            {
                axios.get("/typeTemplate/findTypeTemplateList").then(function (response)
                {
                    vue.typeTemplateList=response.data;
                    vue.itemCat.typeId=vue.typeTemplateList[0].id;
                })
            },
            saveOrUpdate:function ()
            {
                var url="save";
                if(this.itemCat.id)
                {
                    url="update";
                }
                axios.post("/itemCat/"+url,this.itemCat).then(function (response)
                {
                    if(response)
                    {
                        vue.findByParentId(vue.itemCat.parentId);
                    }
                    else
                    {
                        alert("添加或更新失败！");
                    }
                })
            },
            show:function (itemCat)
            {
                this.itemCat=itemCat;
            },
            checkAll:function (event)
            {
                this.ids=[];
                if(event.target.checked)
                {
                    for(var i=0;i<this.itemCatList.length;i++)
                    {
                        this.ids.push(this.itemCatList[i].id);
                    }
                }
            },
            del:function ()
            {
                axios.get("/itemCat/delete?ids="+this.ids).then(function (response)
                {
                    if(response)
                    {
                        vue.findByParentId(vue.itemCat.parentId);
                    }
                    else
                    {
                        alert("删除失败");
                    }
                })
            }
        },
        created:function ()
        {
            this.findByParentId(0);
            this.findTypeTemplateList();
        },
        updated:function ()
        {
            this.checked=(this.ids.length==this.itemCatList.length);
        }
    })
};